<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-title">
        <h3 class="title">集客仓APP后台管理系统</h3>
      </div>
      <div class="form-card">
        <div class="card-content">
          <div class="title">欢迎来到，集客仓！</div>
          <el-form ref="loginForm" :model="loginForm">
            <el-form-item>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
              >
                <div slot="prefix" class="icon-box">
                  <img
                    src="@/assets/login/icon-username.png"
                    alt=""
                    class="input_icon"
                  />
                </div>
              </el-input>
            </el-form-item>
            <el-form-item style="margin-top: 40px">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin"
              >
                <div slot="prefix" class="icon-box">
                  <img
                    src="@/assets/login/icon-password.png"
                    alt=""
                    class="input_icon"
                  />
                </div>
              </el-input>
              <span class="show-pwd" @click="showPwd">
                <svg-icon
                  :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
                />
              </span>
            </el-form-item>
          </el-form>
          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%; height: 60px; background: #4080fa"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import SocialSign from "./components/SocialSignin";

export default {
  name: "Login",
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      callback();
      // if (value.length < 6) {
      //   callback(new Error('The password can not be less than 6 digits'))
      // } else {
      //   callback()
      // }
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    // checkCapslock(e) {
    //   const { key } = e;
    //   this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    // },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url("~@/assets/login/login-bg.png");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center center;
  background-attachment: fixed;
  overflow: hidden;
  position: relative;
  z-index: 0;

  .login-form {
    position: absolute;
    background-position: center center;
    background-attachment: fixed;
    height: 606px;
    width: 1103px;
    z-index: 9;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-40%);
    background: url("~@/assets/login/login-card.png") no-repeat;
    background-size: contain;
    .form-card {
      height: 606px;
      width: 495px;
      position: absolute;
      right: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      .card-content {
        height: 400px;
        width: 347px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .title {
          height: 33px;
          font-size: 32px;
          font-weight: 400;
          color: #1a1a1a;
        }
        .el-form-item {
          position: relative;
          .icon-box {
            border-right: 1px solid #cccccc;
            width: 40px;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            display: flex;
            align-items: center;
            justify-content: center;
            .input_icon {
              width: 20px;
              height: 20px;
            }
          }
          ::v-deep .el-form-item__content {
            height: 60px;
            background: #f3f4f9;
            .el-input__inner {
              height: 60px;
              color: #707070;
              font-size: 16px;
              caret-color: #cccccc;
              padding-left: 60px;
              border: none !important;
              background: transparent !important;
            }
          }
        }
      }
    }
  }

  .login-title {
    position: absolute;
    top: -26%;
    left: 50%;
    display: block;
    transform: translateX(-50%);

    .title {
      white-space: nowrap;
      font-size: 62.3px;
      color: #fff;
      margin: 0px;
      text-align: center;
      font-weight: bold;
      text-shadow: 0 10px 8px rgba(0, 0, 0, 0.3);
    }
  }

  .show-pwd {
    position: absolute;
    right: 15px;
    top: 15px;
    font-size: 16px;
    color: #ccc;
    cursor: pointer;
    user-select: none;
  }
}
</style>
